# windows下进行发行版编译
# cmake -DCMAKE_BUILD_TYPE=Release -GNinja ..
# linux下使用musl库编译
# cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_CXX_COMPILER=/opt/musl/bin/g++ -DCMAKE_C_COMPILER=/opt/musl/bin/gcc -GNinja ..

# cmake需要的版本号
cmake_minimum_required(VERSION 3.10)

# 项目设置
project(miniplayer)

# 设置c和c++标准版本
set(CMAKE_C_STANDARD 11)
set(CMAKE_C_STANDARD_REQUIRED ON)
set(CMAKE_C_EXTENSIONS ON)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

# 全局宏定义  -D_POSIX_C_SOURCE
# add_definitions(-DFMT_HEADER_ONLY -DMG_ENABLE_LOG=0)
add_definitions(-DNO_FMT)

# 设置c和c++编译选项
set(CMAKE_C_FLAGS "-Wall -fPIC")
set(CMAKE_CXX_FLAGS "-Wall -fPIC -fno-rtti -fno-exceptions")
set(CMAKE_C_FLAGS_DEBUG "-O0 -g")
set(CMAKE_CXX_FLAGS_DEBUG "-O0 -g")
set(CMAKE_C_FLAGS_RELEASE "-O2 -flto")
set(CMAKE_CXX_FLAGS_RELEASE "-O2 -flto")

# 设置include和lib的目录
include_directories("include")
link_directories(AFTER "lib")

# windows默认debug模式，linux默认release模式
if (CMAKE_SYSTEM_NAME MATCHES "Windows")
    set(OSN "win")
    if (NOT CMAKE_BUILD_TYPE)
        set(CMAKE_BUILD_TYPE "Debug")
    endif ()
    # add_definitions(-DSPDLOG_COMPILED_LIB -DSPDLOG_ACTIVE_LEVEL=SPDLOG_LEVEL_TRACE -DSPDLOG_FMT_EXTERNAL)
else ()
    message(SEND_ERROR, "unsupport platform ${CMAKE_SYSTEM_NAME}")
endif ()

if(MINGW)
  set(CMAKE_RC_COMPILER_INIT windres)
  ENABLE_LANGUAGE(RC)
  SET(CMAKE_RC_COMPILE_OBJECT "<CMAKE_RC_COMPILER> -i <SOURCE> -o <OBJECT>")
endif(MINGW)

# target_link_libraries(${PROJECT_NAME} PUBLIC mongoose_${OSN}_x64)

# 针对调试版本和发行版本设置编译标志和链接标志
if (CMAKE_BUILD_TYPE AND (CMAKE_BUILD_TYPE STREQUAL "Release"))
    add_definitions(-DNDEBUG)
    add_link_options(-static -s)
else ()
    add_definitions(-DDEBUG)
    # target_link_libraries(${PROJECT_NAME} PUBLIC fmt_${OSN}_x64 spdlog_${OSN}_x64)
endif ()

# aux_source_directory(src/common COMMON_SRCS)
file(GLOB_RECURSE KLIB_SRCS
    ${CMAKE_CURRENT_SOURCE_DIR}/src/klib/*.c
    ${CMAKE_CURRENT_SOURCE_DIR}/src/klib/*.cpp)
file(GLOB_RECURSE KWIN_SRCS
    ${CMAKE_CURRENT_SOURCE_DIR}/src/kwin/*.c
    ${CMAKE_CURRENT_SOURCE_DIR}/src/kwin/*.cpp)

# set(CMAKE_VERBOSE_MAKEFILE ON)
# 或者用 make VERBOSE=1 来显示编译详情
if (CMAKE_SYSTEM_NAME MATCHES "Windows")
    # add_compile_options(-finput-charset=utf-8 -fexec-charset=gbk)
    add_link_options(-Wl,--subsystem,windows)
    link_libraries(comctl32 shlwapi vlc vlccore)
endif()

# 可执行程序
add_executable(miniplayer
    resource/miniplayer.rc
    ${KLIB_SRCS}
    ${KWIN_SRCS}
    src/vlcwnd.cpp
    src/wkewnd.cpp
    src/jsfuncs.cpp
    src/miniplayer.cpp
    src/config.cpp
)
